package cn.yuan.xiaoyu.wangmodule.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.utils.ListUtils;
import cn.yuan.xiaoyu.wangmodule.bean.ContactsBean;

import static cn.yuan.xiaoyu.R.id.tv_item_ww_contact_avatar;
import static cn.yuan.xiaoyu.R.id.tv_item_ww_contact_header;
import static cn.yuan.xiaoyu.R.id.tv_item_ww_contact_name;

/**
 * Created by yukuoyuan on 2017/3/2.
 * 这是一个通讯录界面的列表的适配器
 */

public class WangWangContactsAdapter extends BaseAdapter {
    List<ContactsBean> data;
    Context context;

    public WangWangContactsAdapter(List<ContactsBean> data, Context context) {
        this.data = data;
        this.context = context;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_wangwang_contacts, null);
            myHolder = new MyHolder();
            myHolder.tv_item_ww_contact_header = (TextView) convertView.findViewById(tv_item_ww_contact_header);
            myHolder.tv_item_ww_contact_avatar = (ImageView) convertView.findViewById(tv_item_ww_contact_avatar);
            myHolder.tv_item_ww_contact_name = (TextView) convertView.findViewById(tv_item_ww_contact_name);
            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }
        // 进行分组, 比较上一个拼音的首字母和自己是否一致, 如果不一致, 就显示tv_index

        String currentLetter = data.get(position).getPinyin();
        String indexStr = null;
        if (position == 0) {
            // 1. 如果是第一位
            indexStr = currentLetter;
        } else {
            // 获取上一个拼音
            String preLetter = data.get(position - 1).getPinyin();
            if (!TextUtils.equals(currentLetter, preLetter)) {
                // 2. 当跟上一个不同时, 赋值, 显示
                indexStr = currentLetter;
            }
        }
        myHolder.tv_item_ww_contact_header.setText(currentLetter);
        myHolder.tv_item_ww_contact_header.setVisibility(indexStr == null ? View.GONE
                : View.VISIBLE);
        Glide.with(context).load(data.get(position).getAvatar()).into(myHolder.tv_item_ww_contact_avatar);
        myHolder.tv_item_ww_contact_name.setText(data.get(position).getName());
        return convertView;
    }

    @Override
    public int getCount() {
        if (ListUtils.isEmpty(data)) {
            return 0;
        }
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    class MyHolder {

        TextView tv_item_ww_contact_header;
        ImageView tv_item_ww_contact_avatar;
        TextView tv_item_ww_contact_name;

    }
}
